import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { ElMessage } from "element-plus";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      {
        path: "/profile",
        name: "profile",
        component: () => import("../views/profile.vue"),
        meta: { title: ["个人中心"] },
      },
      {
        path: "/DataVisualization",
        name: "DataVisualization",
        component: () => import("../views/DataVisualization.vue"),
        meta: { title: ["数据可视化"] },
      },
      {
        path: "/PermissionsList",
        name: "PermissionsList",
        component: () => import("../views/PermissionsList.vue"),
        meta: { title: ["用户", "权限列表"] },
      },
      {
        path: "/RoleList",
        name: "RoleList",
        component: () => import("../views/RoleList.vue"),
        meta: { title: ["用户", "角色列表"] },
      },
      {
        path: "/UserList",
        name: "UserList",
        component: () => import("../views/UserList.vue"),
        meta: { title: ["用户", "员工管理"] },
      },
      {
        path: "/import",
        name: "import",
        component: () => import("../views/import.vue"),
        meta: { title: ["用户", "Excel导入"] },
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to,from,next)=>{
  if(to.name!="login" && !localStorage.getItem('token')){
    ElMessage.error("token不存在或已过期,请重新登录")
    router.push("/login");
    return false;
  }
  if(to.name=='login'){
    localStorage.removeItem("routes")
  }
  next()
})

export default router;
